package org.jeecg.modules.zcgl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgBaseController;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.api.enums.TradeStatusEnum;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.zcgl.entity.CustomerBalance;
import org.jeecg.modules.zcgl.entity.CustomerBalanceChangesDetail;
import org.jeecg.modules.zcgl.entity.OrderPrestore;
import org.jeecg.modules.zcgl.enums.AttentionPayStatusEnum;
import org.jeecg.modules.zcgl.enums.HandleTypeEnum;
import org.jeecg.modules.zcgl.enums.PayTypeEnum;
import org.jeecg.modules.zcgl.service.ICustomerBalanceChangesDetailService;
import org.jeecg.modules.zcgl.service.ICustomerBalanceService;
import org.jeecg.modules.zcgl.service.IOrderPrestoreService;
import org.jeecg.modules.zcgl.util.NoUtils;
import org.jeecg.modules.zcgl.vo.CustomerBalancePage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

 /**
 * @Description: 客户余额信息
 * @Author: jeecg-boot
 * @Date:   2020-12-11
 * @Version: V1.0
 */
@Api(tags="客户余额信息")
@RestController
@RequestMapping("/zcgl/customerBalance")
@Slf4j
public class CustomerBalanceController extends JeecgBaseController<CustomerBalancePage, CustomerBalance, ICustomerBalanceService> {
	@Autowired
	private ICustomerBalanceService customerBalanceService;
	@Autowired
	private ICustomerBalanceChangesDetailService customerBalanceChangesDetailService;
	 @Autowired
	 private IOrderPrestoreService orderPrestoreService;
	 @Autowired
	 private ISysDepartService departService;
	
//	/**
//	 * 分页列表查询
//	 *
//	 * @param customerBalance
//	 * @param pageNo
//	 * @param pageSize
//	 * @param req
//	 * @return
//	 */
//	@AutoLog(value = "客户余额信息-分页列表查询")
//	@ApiOperation(value="客户余额信息-分页列表查询", notes="客户余额信息-分页列表查询")
//	@GetMapping(value = "/list")
//	public Result<?> queryPageList(CustomerBalance customerBalance,
//								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
//								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
//								   HttpServletRequest req) {
//		QueryWrapper<CustomerBalance> queryWrapper = QueryGenerator.initQueryWrapper(customerBalance, req.getParameterMap());
//		Page<CustomerBalance> page = new Page<CustomerBalance>(pageNo, pageSize);
//		IPage<CustomerBalance> pageList = customerBalanceService.page(page, queryWrapper);
//		return Result.ok(pageList);
//	}
	@AutoLog(value = "客户余额信息-列表查询")
	@ApiOperation(value="客户余额信息-列表查询", notes="客户余额信息-列表查询")
	@GetMapping(value = "/list")
	public Result<List<CustomerBalance>> queryList(CustomerBalance customerBalance) {
		QueryWrapper<CustomerBalance> queryWrapper = new QueryWrapper<>(customerBalance);
		List<CustomerBalance> list = customerBalanceService.list(queryWrapper);
		return Result.ok(list);
	}
	
	/**
	 *   添加
	 *
	 * @param vo
	 * @return
	 */
	@AutoLog(value = "客户余额信息-根据客户ID和合同ID查询")
	@ApiOperation(value="客户余额信息-根据客户ID和合同ID查询", notes="客户余额信息-根据客户ID和合同ID查询")
	@PostMapping(value = "/selectByCustomerIdAndLeaseContractId")
	public Result<?> selectByCustomerIdAndCompanyId(@RequestBody CustomerBalance vo) {
		QueryWrapper<CustomerBalance> queryWrapper = new QueryWrapper<>(vo);
		CustomerBalance customerBalance = service.selectCustomerIdAndLeaseContractId(vo.getLeaseContractId(), vo.getCustomerId());
		return Result.ok(customerBalance);
	}

	/**
	 *   添加
	 *
	 * @param customerBalancePage
	 * @return
	 */
	@AutoLog(value = "客户余额信息-添加")
	@ApiOperation(value="客户余额信息-添加", notes="客户余额信息-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody CustomerBalancePage customerBalancePage) {
		CustomerBalance customerBalance = new CustomerBalance();
		BeanUtils.copyProperties(customerBalancePage, customerBalance);
		customerBalanceService.saveMain(customerBalance, customerBalancePage.getCustomerBalanceChangesDetailList());
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param vo
	 * @return
	 */
	@AutoLog(value = "客户余额信息-编辑")
	@ApiOperation(value="客户余额信息-编辑", notes="客户余额信息-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody @Validated CustomerBalanceChangesDetail vo) {

		if (vo.getChangesAmount().compareTo(BigDecimal.ZERO) <1){
			return Result.errorValidation("变动金额必须大于0！");
		}
		LoginUser sysUser = JwtUtil.getLoginUser();
		if (!jeecgPropertyConfig.getBaseDepartId().equalsIgnoreCase(sysUser.getCurrentCompanyId())){
			vo.setCompanyId(sysUser.getCurrentCompanyId());
		}
		if (StringUtils.isBlank(vo.getCompanyId())){
			return Result.errorValidation("请选择需要变动的所属公司！");
		}
		if (vo.getChangesType() == null || (vo.getChangesType().intValue() != 1 && vo.getChangesType().intValue() != 2 )){
			return Result.errorValidation("请选择变动类型！");
		}
		boolean bool = vo.getChangesType().intValue() == 1 ? true : false;
		boolean b = customerBalanceService.updateBalanceByCustomerIdAndCompanyId(vo.getCompanyId(),vo.getCustomerId(),
				vo.getChangesAmount(), bool, sysUser.getId(), HandleTypeEnum.HAND_REGULATION.getStatus(), vo.getDescription(),null);
		return b ? Result.ok("修改成功！") : Result.error("修改失败！");
	}

	/**
	 *  编辑
	 *
	 * @param vo
	 * @return
	 */
	@AutoLog(value = "客户余额信息-预存")
	@ApiOperation(value="客户余额信息-预存", notes="客户余额信息-预存")
	@PutMapping(value = "/prestore")
	public Result<?> prestore(@RequestBody CustomerBalanceChangesDetail vo) {

		if (vo.getChangesAmount() == null || vo.getChangesAmount().compareTo(BigDecimal.ZERO) <1){
			return Result.errorValidation("变动金额必须大于0！");
		}
		LoginUser sysUser = JwtUtil.getLoginUser();
		if (!jeecgPropertyConfig.getBaseDepartId().equalsIgnoreCase(sysUser.getCurrentCompanyId())){
			vo.setCompanyId(sysUser.getCurrentCompanyId());
		}
		SysDepart depart = departService.getById(vo.getCompanyId());
		if (StringUtils.isBlank(vo.getCompanyId()) || depart == null){
			return Result.errorValidation("请选择需要预存的所属公司！");
		}

		//生成预存订单
		OrderPrestore orderPrestore = new OrderPrestore();
		orderPrestore.setCompanyId(vo.getCompanyId());
		orderPrestore.setRentCompanyId(vo.getCompanyId());
		orderPrestore.setAmountMoney(vo.getChangesAmount());
		orderPrestore.setPayType(PayTypeEnum.OFFLINE_TRANSFER.getStatus());
		orderPrestore.setPayStatus(AttentionPayStatusEnum.SUCCESSFUL.getStatus());
		orderPrestore.setOrderNo(NoUtils.getOrderNo());
		orderPrestore.setTradeTime(DateUtils.getDate());

		orderPrestore.setCustomerId(vo.getCustomerId());
		orderPrestore.setTradeStatus(TradeStatusEnum.SUCCESS.getKey());
		orderPrestore.setTradeMessage("管理员手动操作预存");
		orderPrestore.setTradeType("线下支付");
		orderPrestore.setPayeeName(depart.getDepartName());
		orderPrestore.setPayeeAccount("");
		orderPrestore.setPayeeBankDeposit("");

		//预存处理
		//boolean b = orderPrestoreService.saveAndCustomerBalance(orderPrestore);
		//return b ? Result.ok("预存成功！") : Result.error("预存失败！");
		return  Result.error("预存失败！");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "客户余额信息-通过id删除")
	@ApiOperation(value="客户余额信息-通过id删除", notes="客户余额信息-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		customerBalanceService.delMain(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "客户余额信息-批量删除")
	@ApiOperation(value="客户余额信息-批量删除", notes="客户余额信息-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.customerBalanceService.delBatchMain(Arrays.asList(ids.split(CommonConstant.DEFAULT_SEPARATOR)));
		return Result.ok("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "客户余额信息-通过id查询")
	@ApiOperation(value="客户余额信息-通过id查询", notes="客户余额信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		CustomerBalance customerBalance = customerBalanceService.getById(id);
		if(customerBalance==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(customerBalance);

	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "客户余额变动明细信息通过主表ID查询")
	@ApiOperation(value="客户余额变动明细信息主表ID查询", notes="客户余额变动明细信息-通主表ID查询")
	@GetMapping(value = "/queryCustomerBalanceChangesDetailByMainId")
	public Result<?> queryCustomerBalanceChangesDetailListByMainId(@RequestParam(name="id",required=true) String id) {
		List<CustomerBalanceChangesDetail> customerBalanceChangesDetailList = customerBalanceChangesDetailService.selectByMainId(id);
		return Result.ok(customerBalanceChangesDetailList);
	}

	 /**
	  * 导出excel
	  *
	  * @param request
	  * @param customerBalance
	  */
	 @RequestMapping(value = "/exportXls")
	 public ModelAndView exportXls(HttpServletRequest request, CustomerBalance customerBalance) {
		 return super.exportXls(request, customerBalance, CustomerBalancePage.class, "客户管理");
	 }

	 /**
	  * 通过excel导入数据
	  *
	  * @param request
	  * @param response
	  * @return
	  */
	 @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	 public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		 return super.importExcel(request, response, CustomerBalancePage.class);
	 }

	 @Override
	 public List<CustomerBalancePage> exportList(List<CustomerBalance> list) {
		 return null;
	 }

	 @Override
	 public boolean importSave(CustomerBalancePage customerBalancePage) {
		 return false;
	 }
 }
